package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddAddressDTO;
import com.nnnu.wsnackshop.pojo.dto.AddressPageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateAddressDTO;
import com.nnnu.wsnackshop.pojo.vo.AddressVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IAddressesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminAddressesController")
@RequestMapping("/api/admin/addresses")
@Slf4j
@Validated
@Tag(name = "管理端-用户收货地址接口")
@RequiredArgsConstructor
public class AddressesController {
    private final IAddressesService addressService;

    @Operation(summary = "分页查询用户地址", description = "仅 superAdmin 或 admin 且拥有 address:read 权限可访问")
    @GetMapping("/page")
    @RequiresRoles(value = {"superAdmin","admin"}, logical = Logical.OR)
    @RequiresPermissions("address:read")
    public Result<?> pageAddresses(
            @Valid @ParameterObject AddressPageDTO dto) {
        log.info("分页查询用户地址：{}", dto);
        return Result.success(addressService.pageAddresses(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取地址详情", description = "仅 superAdmin 或 admin 且拥有 address:read 权限可访问")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin","admin"}, logical = Logical.OR)
    @RequiresPermissions("address:read")
    public Result<AddressVO> getAddress(
            @PathVariable @Parameter(description = "地址 ID", in = ParameterIn.PATH) Long id) {
        log.info("查询地址详情：{}", id);
        AddressVO vo = addressService.getAddressDetail(id);
        return Result.success(vo,MessageConstants.SELECT_OK);
    }

    @Operation(summary = "查询某用户所有地址", description = "仅 superAdmin 或 admin 且拥有 address:read 权限可访问")
    @GetMapping("/list/{userId}")
    @RequiresRoles(value = {"superAdmin","admin"}, logical = Logical.OR)
    @RequiresPermissions("address:read")
    public Result<List<AddressVO>> listByUser(
            @Parameter(description = "用户 ID", in = ParameterIn.PATH) @PathVariable Long userId) {
        log.info("查询用户({})地址列表", userId);
        List<AddressVO> list = addressService.listByUser(userId);
        return Result.success(list,MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增用户地址", description = "仅 superAdmin 且拥有 address:create 权限可访问")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("address:create")
    public Result<Void> createAddress(
            @Valid @RequestBody @Parameter(description = "新增用户地址 DTO") AddAddressDTO dto) {
        log.info("新增用户地址：{}", dto);
        addressService.createAddress(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新用户地址", description = "仅 superAdmin 且拥有 address:update 权限可访问")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("address:update")
    public Result<Void> updateAddress(
            @Valid @RequestBody @Parameter(description = "更新用户地址 DTO") UpdateAddressDTO dto) {
        log.info("更新用户地址：{}", dto);
        addressService.updateAddress(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "批量删除用户地址", description = "仅 superAdmin 且拥有 address:delete 权限可访问")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("address:delete")
    public Result<Void> deleteAddresses(
            @RequestParam @Parameter(description = "地址 ID 列表",in = ParameterIn.QUERY) List<Long> ids) {
        log.info("批量删除用户地址：{}", ids);
        addressService.deleteAddresses(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
